package com.nuanshui.heatedloan.credit.pay.execute;

import com.nuanshui.heatedloan.credit.pay.annotation.PayBehaviorAttribute;
import com.nuanshui.heatedloan.credit.pay.behavior.PayBehavior;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

@Component
public class PayExecuteProcessor implements BeanPostProcessor {


    @Autowired
    private PayExecuteFacade payExecuteFacade;
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {

        Class<?> targetClass = AopUtils.getTargetClass(bean);

        PayBehaviorAttribute attribute =  AnnotationUtils.getAnnotation(targetClass, PayBehaviorAttribute.class);

        if(attribute!=null &&  bean instanceof PayBehavior) {
            PayBehaviorWrapper wrapper = new PayBehaviorWrapper();
            wrapper.setName(attribute.value());
            wrapper.setTarget((PayBehavior)bean);
            wrapper.getTarget().setName(attribute.value());
            wrapper.setOrder(attribute.order());
            payExecuteFacade.register(wrapper);//过滤器注册
        }

        return bean;
    }
//
//    @Override
//    public PayExecuteFacade getObject() throws Exception {
//        return payExecuteFacade;
//    }
//
//    @Override
//    public Class<?> getObjectType() {
//        return PayExecuteFacade.class;
//    }
//
//    @Override
//    public boolean isSingleton() {
//        return true;
//    }

}